package com.todod.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.todod.annotaion.NotLogging;
import com.todod.common.util.Download;
import com.todod.entity.QueryEntry;
import com.todod.entity.ReqCatalog;
import com.todod.entity.ResultData;
import com.todod.mapper.CatalogManageMapper;
import com.todod.model.CatalogManage;
import com.todod.model.User;
import com.todod.service.ICatalogManageService;
import com.todod.service.IUserService;

/**
 * @author dyl
 * @date 2022-09-05
 */
@RestController
@RequestMapping("/catalogManage")
public class CatalogManageController extends BaseManageController {

	@Autowired
	public ICatalogManageService iCatalogManageService;
	@Autowired
	public CatalogManageMapper catalogManageMapper;
	@Autowired
	private IUserService iUserService;

	/**
	 * 
	 * @param companyId
	 * @param _userId
	 * @param request
	 * @param queryEntry
	 * @return
	 */
	@RequestMapping("/getPage")
	public ResultData getListPage(@RequestBody QueryEntry queryEntry) {

		IPage<CatalogManage> page = iCatalogManageService.getListPage(queryEntry);
		return ResultData.success("ok", page);
	}

	/**
	 * 
	 * @param _userId
	 * @param reqCatalogManage
	 * @return
	 */
	@PostMapping("/save")
	public ResultData saveData(@RequestAttribute Long _userId, @RequestBody CatalogManage catalogManage) {

		iCatalogManageService.saveData(_userId, catalogManage);
		return ResultData.success();
	}

	/**
	 * 
	 * @param _userId
	 * @param reqCatalogManage
	 * @return
	 */
	@PostMapping("/edit")
	public ResultData editData(@RequestAttribute Long _userId, @RequestBody CatalogManage catalogManage) {
		iCatalogManageService.editData(_userId, catalogManage);
		return ResultData.success();
	}

	/**
	 * 
	 * @param _userId
	 * @param CatalogManage
	 * @return
	 */
	@PostMapping("/copy")
	public ResultData copyData(@RequestAttribute Long _userId, @RequestBody ReqCatalog reqCatalog) {
		iCatalogManageService.copyData(_userId, reqCatalog);
		return ResultData.success();
	}

	/**
	 * 
	 * @param code
	 * @return
	 */
	@GetMapping("/getInfo")
	public ResultData getInfo(Long code) {
		if (code == null) {
			return ResultData.error(ResultData.PARAM_ERROR_CODE, "参数错误");
		}

		CatalogManage catalogManage = catalogManageMapper.selectById(code);
		return ResultData.success("ok", catalogManage);
	}

	/**
	 * 
	 * @param _userId
	 * @return
	 */
	@RequestMapping("/getListByCode")
	public ResultData getListByCode(@RequestAttribute Long _userId, String code) {
		// 获取权限数据
		List<CatalogManage> list = iCatalogManageService.getListByCode(code);
		return ResultData.success("ok", list);
	}

	/**
	 * @Title: downloadTemplate
	 * @Description: 下载模板文件
	 * @author dyl
	 * @date: 2022-03-07
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("/downloadTemplate")
	@NotLogging
	public ModelAndView downloadTemplate(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String path = "static/file/catalog_detail.xlsx";
		URL excelUrl = getClass().getClassLoader().getResource(path);
		File excelFile = new File(excelUrl.getFile());
		FileInputStream is = new FileInputStream(excelFile);
		Download.download(request, response, is, "目录模板.xlsx");
		return null;
	}

	/**
	 * @Title: uploadFile
	 * @Description:导入文件
	 * @author dyl
	 * @date 2020-04-16
	 * @param request
	 * @param response
	 * @param fileFlag
	 * @return
	 */
	@PostMapping("/uploadFile")
	public ResultData uploadFile(@RequestAttribute Long _userId, HttpServletRequest request,
			HttpServletResponse response, Long classifyId) {

		User user = iUserService.getById(_userId);

		MultipartHttpServletRequest req = (MultipartHttpServletRequest) request;
		MultipartFile file = req.getFile("uploadfile_ant");
		try {
			InputStream is = file.getInputStream();
			// 把excel数据存入到数据库中
			try {
				iCatalogManageService.saveUploadTbData(request, is, user, classifyId);
			} catch (Exception e) {
				return ResultData.error(e.toString().substring(20));
			}

			return ResultData.success("ok");
		} catch (IOException e) {
			e.printStackTrace();
			return ResultData.error("获取上传文件失败");
		}
	}

	/**
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping("/updateState")
	public ResultData updateState(@RequestAttribute Long _userId, Long code, String status) {
		iCatalogManageService.updateState(_userId, code, status);
		return ResultData.success();
	}

}
